﻿
package com.ostrich.manpower.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.page.BasePage;
import com.base.service.BaseService;
import com.ostrich.manpower.bean.SendMsg;
import com.ostrich.manpower.bean.TostHrAccfundinfo;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrCheckinlog;
import com.ostrich.manpower.bean.TostHrDeductinfo;
import com.ostrich.manpower.bean.TostHrFactpayinfo;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
import com.ostrich.manpower.bean.TostHrSalaryinfo;
import com.ostrich.manpower.bean.TostHrSocialsecurity;
import com.ostrich.manpower.bean.TostHrSubsalaryinfo;
import com.ostrich.manpower.bean.TostHrSubsidyinfo;
import com.ostrich.manpower.service.TostHrSalaryinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrSalaryinfoService")
public class TostHrSalaryinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrSalaryinfoService.class);

	@Autowired(required=false)
    private TostHrSalaryinfoDao<T> dao;
	@Autowired(required=false)
	private TostHrMealaddinfoService<TostHrMealaddinfo> tosthrmealaddinfoservice;
	@Autowired(required=false)
	private TostHrCheckinlogService<TostHrCheckinlog> tosthrcheckinlogservice;
	@Autowired(required=false)
	private TostHrAccfundinfoService<TostHrAccfundinfo> tosthraccfundinfoservice;
	@Autowired(required=false)
	private TostHrSocialsecurityService<TostHrSocialsecurity> tosthrsocialsecurityservice;
	@Autowired(required=false)
	private TostHrDeductinfoService<TostHrDeductinfo> tosthrdeductinfoservice;
	@Autowired(required=false)
	private TostHrSubsidyinfoService<TostHrSubsidyinfo> tosthrsubsidyinfoservice;
	@Autowired(required=false)
	private TostHrSubsalaryinfoService<TostHrSubsalaryinfo> tosthrsubsalaryinfoservice;
	@Autowired(required=false)
	private TostHrBaseinfoService<TostHrBaseinfo> tostHrBaseinfoService;
	@Autowired(required=false)
	private TostHrFactpayinfoService<TostHrFactpayinfo> tosthrfactpayinfoservice;

	
	SendMsg sm=new SendMsg();// 传递提示信息的
	public TostHrSalaryinfoDao<T> getDao() {
		return dao;
	}
	public List<T> queryByHrid(Integer hrId) {
		return getDao().queryByHrid(hrId);
	}
	public void deleteByhrMonth(String hrMonth,int hrId){
		dao.deleteByhrMonth(hrMonth,hrId);
	}
	public List<T> queryByHrMonthHrid(String month,int hrId){
		return dao.queryByHrMonthHrid(month, hrId);
	}
	
	
	//这些判断，是在另外三张表里在用的。
	public boolean isHrMealadByMouth(String mouth,int hrId){
		List<TostHrMealaddinfo> list=tosthrmealaddinfoservice.queryIdHridHme(mouth,hrId);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isAccfundinfo(int hrid){
		List<TostHrAccfundinfo> list=tosthraccfundinfoservice.queryAccfund(hrid);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isSocialsecurity(int hrId){
		List<TostHrSocialsecurity> list=tosthrsocialsecurityservice.queryScoci(hrId);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isCheckInlog(int hrId){
		List<TostHrCheckinlog> list=tosthrcheckinlogservice.queryCheckInLogByHrId(hrId);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isSubsidyinfo(int hrId){
		List<TostHrSubsidyinfo> list=tosthrsubsidyinfoservice.querySubsidyinfoByHrId(hrId);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isDeduct(String month,int hrId){
		List<TostHrDeductinfo> list=tosthrdeductinfoservice.queryByHrMonthHrId(month, hrId);
		if(list.size()>0)
			return true;
		else
			return false;
	}
	public boolean isFactPay(String month,int hrId){
		List<TostHrFactpayinfo> list=tosthrfactpayinfoservice.queryByHrMonthHrid(month, hrId);
		if(list.size()>0)
			return true;
		else
			return false;
	}
	public boolean isSubsalary(String month,int hrId){
		List<TostHrSubsalaryinfo> list=tosthrsubsalaryinfoservice.queryByHrMonthHrid(month, hrId);
		if(list.size()>0)
			return true;
		else
			return false;
	}
	public boolean isSalary(String month,int hrId){
		List<TostHrSalaryinfo> list=(List<TostHrSalaryinfo>)this.queryByHrMonthHrid(month, hrId);
		if(list.size()>0)
			return true;
		else
			return false;
	}
	
	//生成其他三种表
	public SendMsg addDeductFactpaySubsalary(String hMonth,int hrid,TostHrSalaryinfo bean) throws Exception{
		List<TostHrBaseinfo> baseinfoList=tostHrBaseinfoService.queryAllById(hrid);
		if(baseinfoList!=null && baseinfoList.size()>0){
			for(TostHrBaseinfo baseinfo : baseinfoList){
				TostHrSubsalaryinfo thsi= tosthrsubsalaryinfoservice.addSubsidyinfo(hMonth, baseinfo);//生成补贴表
				TostHrFactpayinfo thf= tosthrfactpayinfoservice.addFactpay(hMonth, baseinfo);//生成应付表
				TostHrDeductinfo thd= tosthrdeductinfoservice.addDeduc(hMonth, baseinfo);//生成扣款表
				this.addSalaryInfo(thd, thsi, thf,bean,baseinfo);
			}
			sm.setMsg(hMonth+"月份生成成功！");
		}else
			sm.setMsg("数据非法！生成失败！");
		return sm;
	}
	
	//提成绩效没写。
	public void addSalaryInfo(TostHrDeductinfo thd,TostHrSubsalaryinfo thsi,TostHrFactpayinfo thf,TostHrSalaryinfo bean,TostHrBaseinfo bsinfo) throws Exception{
		
		TostHrSalaryinfo ths=new TostHrSalaryinfo();
		
		List<TostHrSalaryinfo> salst=(List<TostHrSalaryinfo>) dao.queryByHrMonthHrid(bean.getHsaMonth(),bsinfo.getId());
		
		ths.setCreatetime(bean.getCreatetime());
		ths.setPerson(bean.getPerson());
		
		ths.setHsaPushmoney(bsinfo.getHrLeavemoney());//提留工资设置
		//设置提成数据信息
		ths.setHsaPerformance(thf.getHfaCommissionwag());
		//设置绩效工资
		ths.setHsaMeritPay(thf.getHfaMeritPay());
		
		if(thd==null||thd.getId()==null){
			System.out.println("扣款数据为空");
		}else if(thsi==null||thsi.getId()==null){
			System.out.println("补贴数据为空");
		}else if(thf==null || thf.getId()==null){
			System.out.println("应付数据为空");
		}else{
			//设置工资表的基础信息  员工id 员工的工资月份  
			ths.setHrId(thd.getHrId());
			ths.setHsaMonth(thd.getHdeMonth());
			
			ths.setHsuId(thsi.getId());//设置工资补贴的id
			ths.setHsuAccount(thsi.getHsuAccount());//设置补贴工资的合计
			
			ths.setHfaId(thf.getId());//设置应发工资的id
			ths.setHfaCount(thf.getHfaCount());//设置应发工资的合计
			
			ths.setHdeId(thd.getId());//设置扣款的表的id
			ths.setHdeDeductcount(thd.getHdeTotal());//设置扣款工资的合计
			
			//实发工资  = 应发工资+补贴合计-扣款合计-个税-餐费补贴-购房车借款
			Float gfc = thd.getHdeBuckleborrow()!=null?thd.getHdeBuckleborrow():new Float(0);//购房车借款
			Float sfgj = thf.getHfaCount()+thsi.getHsuAccount()-thd.getHdeTotal()-thd.getHdePerpayable()-thsi.getHsuMealsubsidies()-gfc;
			ths.setHsaPaid(sfgj);//设置实发金额
			
			ths.setHsaDesc("");//备注 暂无
			ths.setHsaStatus(Integer.toString(0));//工资状态设为生成。
			
			if(salst!=null && salst.size()>0){
				ths.setId(salst.get(0).getId());
				dao.update((T) ths);
			}else
				dao.add((T) ths);
		}
	}
	
	public SendMsg recreate(String hrMonth,int hrId,TostHrSalaryinfo bean) throws Exception{
		//	List<TostHrBaseinfo> baselist=tostHrBaseinfoService.queryAllById(hrId);
		TostHrSalaryinfo salayinfo = (TostHrSalaryinfo) getDao().queryById(hrId);
		if(!this.isSalary(hrMonth, hrId)){
			sm.setMsg(hrMonth+"月份还没生成过，请点击自动生成。");
			return sm;
		}else{
			if(this.isDeduct(hrMonth, hrId)){
				tosthrdeductinfoservice.deleteByhrMonth(hrMonth, hrId);
				if(this.isFactPay(hrMonth, hrId)){
					tosthrfactpayinfoservice.deleteByhrMonth(hrMonth, hrId);
					if(this.isSubsalary(hrMonth, hrId)){
						tosthrsubsalaryinfoservice.deleteByhrMonth(hrMonth, hrId);
						if(this.isSalary(hrMonth, hrId)){
							this.deleteByhrMonth(hrMonth, hrId);
							this.addDeductFactpaySubsalary(hrMonth, hrId,bean);
							sm.setMsg(hrMonth+"月份重新生成成功！");
							return sm;
						}
					}
				}
			}
			else{
				sm.setMsg(hrMonth+"月份的所有表为空，不需要重新重新生成。执行生成方法完成！");
				System.out.println("所有表为空，不需要重新重新生成。执行生成方法！");
				this.addDeductFactpaySubsalary(hrMonth, hrId,bean);
			}
		}
		return sm;
	}
	
	public List<T> queryByMap(TostHrSalaryinfo page) throws Exception{
		Integer rowCount = queryByMapCount(page);
		page.getPager().setRowCount(rowCount);
		return getDao().queryByMap(page);
	}
	public int queryByMapCount(TostHrSalaryinfo page)throws Exception{
		return getDao().queryByMapCount(page);
	}
	public int queryByMapCountMonth(TostHrSalaryinfo page,String startM,String endM)throws Exception{
		return getDao().queryByMapCountMonth(page,startM,endM);
	}
	//查询时间区间
	public List<T> queryByMonth(TostHrSalaryinfo tostHrSalaryinfo,String startM,String endM) throws Exception{
		Integer rowCount = queryByMapCountMonth(tostHrSalaryinfo, startM, endM);
		tostHrSalaryinfo.getPager().setRowCount(rowCount);
		return getDao().queryByMonth(tostHrSalaryinfo, startM, endM);
	}
	
	
	
	@Override
	public void delete(Object... ids) throws Exception{
		if(ids == null || ids.length < 1){
			return;
		}
		for (Object id : ids) {
			TostHrSalaryinfo salayinfo = (TostHrSalaryinfo) getDao().queryById(id);
			// 删除扣款表内容
			if (salayinfo.getHdeId() != null)
				tosthrdeductinfoservice.delete(salayinfo.getHdeId());
			// 删除应发表id内容信息
			if (salayinfo.getHfaId() != null)
				tosthrfactpayinfoservice.delete(salayinfo.getHfaId());
			// 删除补贴表id内容信息
			if (salayinfo.getHsuId() != null)
				tosthrsubsalaryinfoservice.delete(salayinfo.getHsuId());
			getDao().delete(id);
		}
	}
	public String querymaxMonth(){
		return getDao().querymaxMonth();
	}
	public String queryminMonth(){
		return getDao().queryminMonth();
	}
	public List<T> querySalaryDerpart(TostHrSalaryinfo tosthrsalaryinfo,String derpart,String startM,String endM){
		return getDao().querySalaryDerpart(tosthrsalaryinfo, derpart,startM, endM);
	}
}
